﻿namespace IntellectShop.Web2.Hardware.Controllers
{
	using System;
	using System.Web;
	using System.Web.Mvc;
	using IntellectShop.Common;
	using IntellectShop.Web2.Hardware.ViewModels;

	[IntellectShop.Web2.Hardware.Code.NoCache]
	[IntellectShop.Web2.Hardware.Code.HandleErrorWithELMAH]
	[ValidateInputAttribute(false)]
	public class CategoryController : Controller
	{
		public ActionResult Index()
		{
			var viewModel = new CategoriesViewModel
			{
				Categories = Common.Category.RetrieveAll(),
			};

			return View(viewModel);
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Create()
		{
			return View(new Common.Category());
		}

		[HttpPost]
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Create(Common.Category category)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Category!");

				category.Create(HttpContext.User.Identity.Name);
				return RedirectToAction("Index");
			}
			catch
			{
				return View(new Common.Category());
			}
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int id)
		{
			var category = Common.Category.RetrieveByID(id);
			if (category == null)
				throw new HttpException(404, "Page not found");

			return View(category);
		}

		[HttpPost]
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int id, FormCollection collection)
		{
			var category = Common.Category.RetrieveByID(id);
			if (category == null)
				throw new HttpException(404, "Page not found");

			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Category!");

				UpdateModel(category);
				category.Update(HttpContext.User.Identity.Name);
				return Redirect(Url.RouteUrl(new { action = "Index" }) + "#id" + id);
			}
			catch
			{
				return View(category);
			}
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int categoryID)
		{
			var category = Common.Category.RetrieveByID(categoryID);
			if (category == null)
				throw new HttpException(404, "Page not found");
			var viewModel = new CategoryRemoveViewModel
			{
				Category = category
			};

			return View(viewModel);
		}

		[HttpPost]
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int categoryID, FormCollection collection)
		{
			var category = Common.Category.RetrieveByID(categoryID);
			if (category == null)
				throw new HttpException(404, "Page not found");

			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Category!");

				Common.Category.Delete(categoryID);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new CategoryRemoveViewModel
				{
					Category = category,
					Message = e.Message
				};

				return View(viewModel);
			}
		}
	}
}
